package com.jiankangjin.alipaychannel.controller;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.alipaychannel.services.impl.QueryServiceImpl;
import com.jiankangjin.alipaychannel.util.StringUtil;
import com.jiankangjin.alipaychannel.vo.QueryVo;

/**
 * 统一支付渠道-订单查询接口
 */
@Controller
public class QueryController {
	private static final transient Log logger = LogFactory.getLog(QueryController.class);
	
	/**
	 * 转向微信查询单笔订单接口
	 */
	@RequestMapping(value="/query", method=RequestMethod.POST)
	@ResponseBody
	public QueryVo query (
			@RequestParam(required = false) String channel_fee_type,
			@RequestParam(required = false) String version,
			@RequestParam(required = false) String pay_ssn_id,
			@RequestParam(required = false) String order_id,
			@RequestParam(required = false) String pay_chnl_seq,
			@RequestParam(required = false) String expand_json,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("channel_fee_type=" + channel_fee_type);
		sb.append(" version=" + version);
		sb.append(" pay_ssn_id=" + pay_ssn_id);
		sb.append(" order_id=" + order_id);
		sb.append(" pay_chnl_seq=" + pay_chnl_seq);
		sb.append(" expand_json=" + expand_json);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("支付接口：" + sb);
		}
		QueryVo queryVo = new QueryVo();
		//验证有效性
		if (StringUtil.isEmpty(channel_fee_type)) {
			queryVo.setResp_msg("channel_fee_type不能为空");
			return queryVo;
		}
		if (StringUtil.isEmpty(version)) {
			queryVo.setResp_msg("version不能为空");
			return queryVo;
		}
		if (StringUtil.isEmpty(pay_ssn_id)) {
			queryVo.setResp_msg("pay_ssn_id不能为空");
			return queryVo;
		}
		if (StringUtil.isEmpty(order_id)) {
			queryVo.setResp_msg("order_id不能为空");
			return queryVo;
		}
		if (StringUtil.isEmpty(pay_chnl_seq)) {
			queryVo.setResp_msg("pay_chnl_seq不能为空");
			return queryVo;
		}
		if (!channel_fee_type.equalsIgnoreCase("public") && 
				!channel_fee_type.equalsIgnoreCase("barcode") && 
				!channel_fee_type.equalsIgnoreCase("default")) {
			queryVo.setResp_msg("channel_fee_type必须为public barcode default 不区分大小写");
			return queryVo;
		}
		if (!version.equalsIgnoreCase("V0.0.1") && 
				!version.equalsIgnoreCase("V0.1") && 
				!version.equalsIgnoreCase("V1")) {
			queryVo.setResp_msg("version必须类似于0.0.1 V0.1 V1 不区分大小写");
			return queryVo;
		}
		//验证签名
		
		//支付类型和版本路由
		try {
			String methodname = channel_fee_type + "_" +version.toUpperCase().replace(".", "_");
			Method transMethod = QueryServiceImpl.class.getMethod(methodname, new Class[]{String.class,String.class,String.class,String.class,});
			return (QueryVo) transMethod.invoke(QueryServiceImpl.class.newInstance(), pay_ssn_id,
					order_id, pay_chnl_seq, expand_json);
		} catch (Exception e) {
			queryVo.setResp_msg("支付类型和版本路由出错");
			e.printStackTrace();
		}
		return queryVo;
	}
}
